package pl.uz.zgora.plan;

import java.io.IOException;

import org.ksoap2.SoapFault;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
 * 
 * @author arti
 * Widok "Ustawienia zaawansowane"
 */
public class AdvancedSettingsActivity extends Activity {

	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.advanced_settings_view);
	    // Pole tekstowe przechowujące adres pliku WSDL
	    final TextView wsdlLocation = (TextView) findViewById(R.id.settings_tv_wsdl_location);
	    final PrefrencesManager pm = PrefrencesManager.getInstance(this);
		// Adres pliku WSDL
	    final String url = pm.getStringFromPreferences("wsdl_url");
		if(url != null) {
			wsdlLocation.setText(url);
		} else {
			wsdlLocation.setText(SOAPManager.getServiceURL());
		}
	    
	    
	    final ProgressDialog progressDialog = HelperMethodsManager.getProgressDialog(this);
	    final SOAPManager soapManager = new SOAPManager(this);
	    // Przycisk "Przywróć domyślne"
	    final Button restoreBtn = (Button) findViewById(R.id.adv_settings_restore);
	    restoreBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				wsdlLocation.setText(SOAPManager.getServiceURL());
			}
		});
	    // Przycisk "Zapisz ustawienia"
	    final Button saveBtn = (Button) findViewById(R.id.adv_settings_save);
	    saveBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				PrefrencesManager pm = PrefrencesManager.getInstance(v.getContext());
				pm.addStringToPreferences("wsdl_url", wsdlLocation.getText().toString());
				HelperMethodsManager.showToast(v.getContext(), "Adres zapisany");
			}
		});
	    
	    final Button refreshPersDataBtn = (Button)findViewById(R.id.adv_settings_refresh_pers);
	    refreshPersDataBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(final View view) {
				
				final Thread downloadThread = new Thread(new Runnable() {
		    		
		    		public void run() {
		    			try {
		    				progressDialog.show();
							soapManager.downloadTeachersIDFromPers(view.getContext());
							progressDialog.hide();
						} catch (SoapFault e) {
							progressDialog.hide();
							HelperMethodsManager.manageException("downloadTeachersIDFromPers()", e.getLocalizedMessage(), AdvancedSettingsActivity.this);
						} catch (IOException e) {
							progressDialog.hide();
							HelperMethodsManager.manageException("downloadTeachersIDFromPers()", e.getLocalizedMessage(), AdvancedSettingsActivity.this);
						} catch (XmlPullParserException e) {
							progressDialog.hide();
							HelperMethodsManager.manageException("downloadTeachersIDFromPers()", e.getLocalizedMessage(), AdvancedSettingsActivity.this);
						}
		    		}
				});
				downloadThread.start();
			}
		});
	}
}
